package com.skplanet.oneplusonewindow.moimserver.controller.api;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.skplanet.oneplusonewindow.moimserver.domain.Moim_Board;
import com.skplanet.oneplusonewindow.moimserver.domain.Moim_Comment;
import com.skplanet.oneplusonewindow.moimserver.service.BoardService;


@Controller
public class BoardAPIController {
	
	@Autowired
	private BoardService boardService;

	@ResponseBody	
	@RequestMapping( value = { "/json/member/boardList", "/json/member/boardList.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> boardList(
			@RequestParam(value="memberId", required=true)long memberId) {	
		
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		mapJSON.put("boars", boardService.getBoardList(memberId));
		return mapJSON;
	} 

	@ResponseBody
	@RequestMapping( value = { "/json/member/writeBoard", "/json/member/writeBoard.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> writeBoard(
			@RequestParam(value="memberId", required=true)long memberId,
			@RequestParam(value="title", required=true)String title,
			@RequestParam(value="content", required=true)String content
	){
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		Moim_Board board = new Moim_Board();
		board.setTitle(title);
		board.setContent(content);
		boardService.addBoard(memberId, board);
		
		mapJSON.put("result", "success");
		return mapJSON;
	}	
	
	@ResponseBody
	@RequestMapping( value = { "/json/member/updateBoard", "/json/member/updateBoard.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> updateBoard(
			@RequestParam(value="boardId", required=true)long boardId,
			@RequestParam(value="title", required=true)String title,
			@RequestParam(value="content", required=true)String content
	){
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		Moim_Board board = new Moim_Board();
		board.setTitle(title);
		board.setContent(content);
		boardService.updateBoard(boardId, board);
		
		mapJSON.put("result", "success");
		return mapJSON;
	}	
	
	@ResponseBody
	@RequestMapping( value = { "/json/member/deleteBoard", "/json/member/deleteBoard.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> deleteBoard(
			@RequestParam(value="boardId", required=true)long boardId
	){
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		Moim_Board board = boardService.getBoard(boardId);
		boardService.deleteBoard(board);
		
		mapJSON.put("result", "success");
		return mapJSON;
	}
	
	@ResponseBody	
	@RequestMapping( value = { "/json/member/commentList", "/json/member/commentList.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> commentList(
			@RequestParam(value="boardId", required=true)long id) {	
		
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		mapJSON.put("boars", boardService.getCommentList(id));
		return mapJSON;
	} 
	@ResponseBody
	@RequestMapping( value = { "/json/member/writeComment", "/json/member/writeComment.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> writeComment(
			@RequestParam(value="boardId", required=true)long boardId,
			@RequestParam(value="content", required=true)String content,
			@RequestParam(value="userId", required=true)long userId
	){
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		Moim_Comment comment = new Moim_Comment();
		comment.setContent(content);
		comment.setMemberId(userId);
		boardService.addComment(boardId, comment);
		
		mapJSON.put("result", "success");
		return mapJSON;
	}	
	
	@ResponseBody
	@RequestMapping( value = { "/json/member/updateComment", "/json/member/updateComment.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> updateComment(
			@RequestParam(value="commentId", required=true)long commentId,
			@RequestParam(value="content", required=true)String content
	){
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		Moim_Comment comment = new Moim_Comment();
		comment.setContent(content);
		boardService.updateComment(commentId, comment);
		
		mapJSON.put("result", "success");
		return mapJSON;
	}	
	
	@ResponseBody
	@RequestMapping( value = { "/json/member/deleteComment", "/json/member/deleteComment.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> deleteComment(
			@RequestParam(value="commentId", required=true)long commentId
	){
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		Moim_Comment comment = boardService.getComment(commentId);
		
		boardService.deleteComment(comment);
		
		mapJSON.put("result", "success");
		return mapJSON;
	}
	

	@ResponseBody	
	@RequestMapping( value = { "/json/member/getBoard", "/json/member/getBoard.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> getBoard(
			@RequestParam(value="boardId", required=true)long boardId) {	
		
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		mapJSON.put("boars", boardService.getBoard(boardId));
		return mapJSON;
	} 
}
